package com.rop.znyx.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rop.znyx.activity.mapper.SecKillMapper;
import com.rop.znyx.activity.service.SecKillService;
import com.rop.znyx.model.activity.SecKill;
import com.rop.znyx.vo.activity.SecKillQueryVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/7/11
 * Time: 9:42
 * Description: 秒杀管理
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class SecKillServiceImpl extends ServiceImpl<SecKillMapper, SecKill> implements SecKillService {

    /**
     * 获取秒杀的分页列表
     */
    @Override
    public IPage<SecKill> selectPage(Page<SecKill> pageParam, SecKillQueryVo SecKillQueryVo) {
        // 查询条件
        String title = SecKillQueryVo.getTitle();
        Integer status = SecKillQueryVo.getStatus();

        // 查询
        LambdaQueryWrapper<SecKill> queryWrapper = new LambdaQueryWrapper<>();
        Optional.ofNullable(title).ifPresent(t -> queryWrapper.eq(SecKill::getTitle, title));
        Optional.ofNullable(status).ifPresent(t -> queryWrapper.like(SecKill::getStatus, status));

        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 更新状态
     */
    @Override
    @Transactional
    public boolean updateStatus(Long id, Integer status) {
        SecKill SecKill = new SecKill();
        SecKill.setId(id);
        SecKill.setStatus(status);

        return this.updateById(SecKill);
    }
}
